<?php
class App_Zend_Controller_Action_Shop extends Zend_Controller_Action
{
	public function init()
	{
		$this->view->keyword = $this->getRequest()->getParam('keyword');
		if ($this->getRequest()->getParam('controller') != 'sell') {
			
			$invId = isset($_COOKIE[COOKIE_PRE.'inv_facebook_id']) ? $_COOKIE[COOKIE_PRE.'inv_facebook_id'] : 0;
			$myId = isset($_COOKIE[COOKIE_PRE.'my_facebook_id']) ? $_COOKIE[COOKIE_PRE.'my_facebook_id'] : 0;
			$pattern = '/\d+/';
			
			$flag = 0;
			
			if(!empty($invId) && preg_match($pattern, $invId)) { //邀请id合法
				$flag += 1;				
			}
			
			if(!empty($myId) && preg_match($pattern, $myId)) { //自己合法
				$flag += 1;
			}
			
			if ($invId != $myId) {
				//$flag += 1;				
			}
					
			if ($flag == 2) { //应用过来合法用户
				define('SELL_VALIDATE', 1);
				define('SELL_OFF', 0.1);
				define('SELL_BONUS', 0.05);
				if ($invId == $myId) {
					define('IS_SELF', 1);				
				}
			}			
		}
	}
	
 	public function preDispatch()
    {
        $this->view->addHelperPath(MY_ROOT.'/vhelpers');
        Zend_Controller_Action_HelperBroker::addPath(MY_ROOT.'/chelpers');
        $this->getHelper('Nav')->getHeaderNav();
        $this->getHelper('User')->getinfo($this->_request->getModuleName(),$this->_request->getControllerName());
		$this->view->assign ( "shopCurUrl", $this->getCurUrl() );
		
		$myCache = Zend_Registry::get('myCache');
		$popArr = $myCache->load("keywordsPopSearch");
		$keyModel = new CKeywords();
		if(!$popArr){
			$popArr = $keyModel->getHotSearch(8);
			$myCache->save($popArr,'keywordsPopSearch');
		}
		$hotkeywords = $myCache->load("keywordsHotSearch");
		if(!$hotkeywords){
			$hs = new CHotSearch();
			$hotkeywords = $hs->getDisplay();
			$myCache->save($hotkeywords,'keywordsHotSearch');
		}
		$this->getHelper('Fenlei')->getCategories();
    	$this->view->hotkeywords = $hotkeywords;
		$this->view->popSearch = $popArr;
		$this->view->keyModel = $keyModel;
    }
	
	private function getCurUrl()
	{
		if(!empty($_SERVER["REQUEST_URI"])){
			$scriptName = $_SERVER["REQUEST_URI"];
			$nowurl = $scriptName;
		}else{
			$scriptName = $_SERVER["PHP_SELF"];
			if(empty($_SERVER["QUERY_STRING"])){
				$nowurl = $scriptName;
			}else{ 
				$nowurl = $scriptName."?".$_SERVER["QUERY_STRING"];
			}
		}
		return urlencode($nowurl);
	}
}